package service;

import dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pojo.User;

import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    public User login(String userCode, String userPassword) {
        User user = userMapper.getLoginUser(userCode);
        // 匹配密码
        if (null != user) {
            if (!user.getUserPassword().equals(userPassword))
                user = null;
        }
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<User> getUserList(String queryUserName, Integer queryUserRole,
                                  int currentPageNo, int pageSize) {
        return userMapper.getUserList(queryUserName, queryUserRole, currentPageNo,
                pageSize);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public User selectUserCodeExist(String userCode) {
        return userMapper.getLoginUser(userCode);
    }

    public boolean deleteUserById(Integer delId) {
        return userMapper.deleteUserById(delId) > 0;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public User getUserById(String id) {
        return userMapper.getUserById(id);
    }

    public boolean modify(User user) {
        return userMapper.modify(user) > 0;
    }

    public boolean updatePwd(int id, String pwd) {
        return userMapper.updatePwd(id, pwd) > 0;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public int getUserCount(String queryUserName, int queryUserRole) {
        return userMapper.getUserCount(queryUserName, queryUserRole);
    }

    @Override
    public boolean add(User user) {
        return userMapper.add(user)>0;
    }

}
